package com.yougong.api.modules.customer.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yougong.api.enums.ErrorEnum;
import com.yougong.api.modules.customer.model.dto.*;
import com.yougong.api.modules.customer.model.pojo.CusUser;
import com.yougong.api.modules.customer.service.CusCacheService;
import com.yougong.api.modules.customer.service.CusUserService;
import com.yougong.common.api.CommonPage;
import com.yougong.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 用户相关
 */
@RestController
@RequestMapping("/cusUser")
@Api(tags = "用户相关操作")
public class CusUserController {

    @Autowired
    private CusUserService cusUserService;

    @Autowired
    private CusCacheService cacheService;

    /**
     * 保存用户
     * @param cusUser
     * @return
     */
    @PostMapping("/save")
    @ApiOperation("保存用户")
    @SaCheckPermission("role-saveUser")
    public CommonResult saveUser(@RequestBody CusUser cusUser){

        return  cusUserService.saveUser(cusUser);
    }

    /**
     * 修改用户
     * @param userDto
     * @return
     */
    @PutMapping()
    @ApiOperation("修改用户")
    @SaCheckPermission("common")
    public CommonResult updateUser( @RequestBody UserDto userDto){

        return cusUserService.updateUser(userDto);
    }

    /** 
    *  获取用户信息
    * @date: 2022/4/21 
    * @author: MuChen
    */ 
    @GetMapping("read/{uuid}")
    @SaCheckPermission("common")
    public CommonResult read(@PathVariable String uuid){
        //参数校验
        if (StringUtils.isEmpty(uuid)){
            return CommonResult.failed(ErrorEnum.REQUEST_ARGUMENTS_EMPTY);
        }
        CusUser cusUser = cusUserService.getById(uuid);
        return CommonResult.success(cusUser);
    }

    /**
     *获取所有的用户信息
     * @param userListDto
     * @return
     */
    @PostMapping("/all")
    @ApiOperation("获取所有的用户信息")
    @SaCheckPermission("user-getCusUserList")
    public CommonResult<CommonPage<CusUser>> getCusUserList(@Validated @RequestBody UserListDto userListDto){

        if (ObjectUtils.isEmpty(userListDto)){
            return CommonResult.failed(ErrorEnum.REQUEST_ARGUMENTS_EMPTY);
        }
        return cusUserService.getCusUserList(userListDto);
    }

    /**
     * 登出
     * @return
     */
    @GetMapping("/logout")
    @ApiOperation("登出")
    public CommonResult logout() {
        return CommonResult.success(null);
    }

    /**
     * 登录 无邮件
     * @param loginDto
     * @return
     */
    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/login")
    public CommonResult login(@Validated @RequestBody LoginDto loginDto) {

        //判断验证码
        /*CaptchaDTO cache = cacheService.getCache(loginDto.getCaptchaId());

        if(cache == null || !cache.getCaptchaValue().equals(loginDto.getCode())){
            return CommonResult.failed(ErrorEnum.CAPTCHA_WRONG);
        }*/

        return cusUserService.login(loginDto.getUsername(), loginDto.getPassword());
    }


    /**
     * 登录 发送邮件
     * @param loginDto
     * @return
     */
    @ApiOperation(value = "登录以后返回token")
    @PostMapping("/emailLogin")
    public CommonResult emailLogin(@Validated @RequestBody LoginDto loginDto) {

        //判断验证码
        CaptchaDTO cache = cacheService.getCache(loginDto.getCaptchaId());

        if(cache == null || !cache.getCaptchaValue().equals(loginDto.getCode())){
            return CommonResult.failed(ErrorEnum.CAPTCHA_WRONG);
        }

        return cusUserService.emailLogin(loginDto);
    }


    /**
     * 根据用户id获取积分
     * @param uuid
     * @return
     */
    @GetMapping("/getScore/{uuid}")
    @ApiOperation("根据用户id获取积分")
    @SaCheckPermission("user-getByUUid-user")
    public CommonResult getByUUid(@ApiParam(name = "用户id")@PathVariable String uuid){

        if (StringUtils.isEmpty(uuid)){
            return CommonResult.failed(ErrorEnum.REQUEST_ARGUMENTS_ERROR);
        }

        CusUser cusUser = cusUserService.getById(uuid);

        if (cusUser == null){
            return CommonResult.failed(ErrorEnum.NOT_USER);
        }

        HashMap<String, Object> map = new HashMap<>();

        return CommonResult.success(map);
    }

    /**
     * 根据用户id修改密码
     * @param passWordDto
     * @return
     */
    @PutMapping("/updatePassword")
    @ApiOperation("根据用户id修改密码")
    @SaCheckPermission("admin-updatePassword")
    public CommonResult updatePassword(@Validated @RequestBody PassWordDto passWordDto){

        return cusUserService.updatePassword(passWordDto);

    }

    /**
     * 批量修改用户状态 1：启用 -1：禁用
     * @param statusDtoList
     * @return
     */
    @PostMapping("/updateStatus")
    @ApiOperation("批量修改用户状态")
    @SaCheckPermission("user-updateStatus")
    public CommonResult updateStatus(@RequestBody List<StatusDto> statusDtoList){

        if (ObjectUtils.isEmpty(statusDtoList)){
            return CommonResult.failed(ErrorEnum.REQUEST_ARGUMENTS_EMPTY);
        }

        return cusUserService.updateStatus(statusDtoList);
    }


    /**
     * 获取验证码
     * @param captchaId
     * @return
     */
    @ApiOperation(value = "获取验证码")
    @GetMapping("/getVerificationCode")
    public ResponseEntity<byte[]> getVerificationCode(String captchaId) {
        try {
            return cusUserService.getVerificationCode(captchaId);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity(CommonResult.failed("验证码请求失败"), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     *发送邮箱验证码
     * @param emailDto
     * @return
     */
    @PostMapping("/sendEmail")
    public CommonResult sendEmail(@Validated @RequestBody EmailDto emailDto){

        return cusUserService.checkVerificationCode(emailDto);

    }

    /**
     * 校验验证码
     * @param captchaDTO
     * @return
     */
    @PostMapping("/checkVerification")
    public CommonResult checkVerification( @RequestBody CaptchaDTO captchaDTO){
        //判断验证码
        CaptchaDTO cache = cacheService.getCache(captchaDTO.getCaptchaId());

        if(cache == null || !cache.getCaptchaValue().equals(captchaDTO.getCaptchaValue())){
            return CommonResult.failed(ErrorEnum.CAPTCHA_WRONG);
        }

        return CommonResult.success(null);
    }

    /**
     * 获取邮箱验证码
     * @param emailCodeDto
     * @return
     */
    @PostMapping("/getEmailCode")
    public CommonResult getEmailCode(@Validated @RequestBody EmailCodeDto emailCodeDto){

        return cusUserService.getEmailCode(emailCodeDto);
    }

    /**
     * 验证邮箱验证码 重置密码
     * @return
     */
    @PostMapping("/resetPassword")
    public CommonResult resetPassword(@Validated @RequestBody EmailCodeDto emailCodeDto){
        return cusUserService.resetPassword(emailCodeDto);
    }


}
